<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>06_列表渲染_过滤与排序</title>
</head>
<body>
<!--
1. 列表过滤
2. 列表排序
-->

<div id="demo">
	<input type="text" v-model="searchName"/><br>
	<ul>
		<li v-for="(p,i) in filterPerson" :key="i">
			{{i}}--{{p.name}}--{{p.age}}
		</li>
	</ul>
	<button @click="setType(0)">原排序(年龄)</button>
	<button @click="setType(1)">升序(年龄)</button>
	<button @click="setType(2)">降序(年龄)</button>
</div>


<script src="../js/vue.js"></script>
<script>
	const vm = new Vue({
		el: '#demo',
		data: {
		   searchName: '',
		   type: 0, // 0代表不排序, 1代表降序, 2代表升序 
           persons: [
              {name: 'Tom', age:18},
              {name: 'Jack', age:17},
              {name: 'Bob', age:19},
              {name: 'Mary', age:16}
      ]
		},
		computed: {
			filterPerson: function(){
				// 取出相关数据
				const {searchName,persons,type} = this;
       
                // 过滤数组
				let filterPerson;
				filterPerson = persons.filter(p => p.name.indexOf(searchName)>=0);
				// 排序
				if(type!=0){
					filterPerson.sort((p1,p2) =>{
                        if(type ==2){// 降序
                        	return p2.age - p1.age;
                        }else{// 升序
                        	return p1.age -p2.age;
                        }

					})
				}
				return filterPerson;
			}
		},
		methods: {
			setType: function(type){
				this.type = type;
			}
		}
	})
</script>
</body>
</html>